package com.openstack4j.qx.imp;

import org.openstack4j.api.OSClient.OSClientV2;
import org.openstack4j.api.networking.NetworkingService;
import org.openstack4j.api.networking.ext.LbaasV2Service;
import org.openstack4j.api.networking.ext.LoadBalancerV2Service;
import org.openstack4j.model.common.ActionResponse;
import org.openstack4j.model.network.ext.LoadBalancerV2;

import com.openstack4j.qx.LoadBalanceController;
import com.openstack4j.qx.pojo.ApplyLoadBalanceResp;
import com.openstack4j.qx.pojo.BaseResp;
import com.openstack4j.qx.pojo.LoadBalanceTemplate;
import com.openstack4j.qx.pojo.ModifyLoadBalanceResp;

/**
 * ClassName:LoadBalanceControllerImpl
 * Function: 负载均衡接口实现类. 
 * Reason:	 实现定义在负载均衡接口中的方法.
 * Create Date:  2016年12月6日:上午11:02:00
 * Author:       yuanzhijin
 * @see 	 
 */

public class LoadBalanceControllerImpl extends BaseControllerImp implements LoadBalanceController {
	
	OSClientV2 os ;
	

	@Override
	public ApplyLoadBalanceResp applyLoadBalance(LoadBalanceTemplate loadBalanceTemplate) {
		
		ApplyLoadBalanceResp resp = new ApplyLoadBalanceResp();
		try {
			os = super.login();
			
			NetworkingService networkingService = os.networking();
			
			LbaasV2Service lbaasV2Service = networkingService.lbaasV2();
			
			//申请负载业务实现
			
		} catch (Exception e) {
			resp.setFaultstring(e.getMessage());
		}
		return resp;
	}
	
	
	/**
	 *取消负载均衡业务服务
	 *参数说明： @param lbID 负载均衡业务编码ID
	 *通过传入负载均衡业务编码id取消负载均衡服务
	 */
	@Override
	public BaseResp cancelLoadBalance(String lbID) {
		
		BaseResp resp =new BaseResp();
		try {
			os = super.login();
			
			NetworkingService networkingService = os.networking();
			
			LbaasV2Service lbaasV2Service = networkingService.lbaasV2();
			
			LoadBalancerV2Service loadBalancerV2Service = lbaasV2Service.loadbalancer();
			
			ActionResponse actionResponse = loadBalancerV2Service.delete(lbID);
			
			if(!actionResponse.isSuccess())
				resp.setFaultstring(actionResponse.getFault());
			
		} catch (Exception e) {
			resp.setFaultstring(e.getMessage());
		}
		return resp;
	}

	
	/**
	 *取消负载均衡业务服务
	 *参数说明： @param lbID 负载均衡业务编码ID
	 *通过传入负载均衡业务编码id取消负载均衡服务
	 *@todo 有参数需要协商
	 */
	@Override
	public ModifyLoadBalanceResp modifyLoadBalance(LoadBalanceTemplate loadBalanceTemplate) {
		ModifyLoadBalanceResp resp =new ModifyLoadBalanceResp();
		try {
			os = super.login();
			
			NetworkingService networkingService = os.networking();
			
			LbaasV2Service lbaasV2Service = networkingService.lbaasV2();
			
			LoadBalancerV2Service loadBalancerV2Service = lbaasV2Service.loadbalancer();
			
			// 需要确定欲修改的负载均衡业务资源编码ID,
			LoadBalancerV2 loadBalancerV2 =loadBalancerV2Service.get(loadBalanceTemplate.getLbID());
		 
 			//修改负载均衡业务编写
			
		} catch (Exception e) {
			resp.setFaultstring(e.getMessage());
		}
		return resp;
	}
	
	

}

